package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/*
    用户的Activity，分为个人中心和查询、申请两部分。
    还在编写，代码可能有些问题。
 */
public class StudentActivity extends AppCompatActivity {
    /*
    适配器
     */

    /*
    用于Log的Tag
     */
    private final String Tag = "UserActivity";

    /*
    控件
     */

    //个人中心的控件
    private TextView user_center_character;
    private TextView user_id;
    private TextView user_name;
    private TextView user_pwd;
    private TextView user_viewpwd;
    private Button user_logout;


    //查询、申请界面的控件
    private Spinner spinner_time;
    private Spinner spinner_name_building;
    private Spinner spinner_crid;
    private Spinner spinner_classtime;
    private Button button_search;
    private Button button_apply;
    private ListView listView;
    private TextView time_applied;
    private TextView user_sa_textview1;
    private TextView user_sa_textview2;

    /*
    由于代码设计出现问题，需要在StudentActivity中使用的、关于用户申请的数据String类型的变量applied现在在ClassroomInfo_Adapter类中产生，且该变量无法送出
    于是设计了静态的方法，从ClassroomInfo_Adapter类中调用该方法设置applied变量
     */
    private static String user_applied;
    public static void setApplied(String applied){
        user_applied = applied;
    }
    /*
    各种要用到的属性
     */

    ArrayList<String> id_cr_all = new ArrayList<>();//给教室号下拉框的输入值
    private user_info info = new user_info("id","pwd","name","character");//接收LoginActivity传过来的个人信息
    private String date_selected;//下拉框中选定的时间
    private String name_building_selected;//下拉框中选定的教学楼
    private String classtime_selected;//下拉框中选定的课时
    private String crid_selected;//下拉框中选定的教室号


    /*
    从数据库中取出教室的数据
     */
    private ArrayList<classroom_info> db_classroom_1 = new ArrayList<>();//今天的教室数据
    private ArrayList<classroom_info> db_classroom_2 = new ArrayList<>();//明天的教室数据
    private ArrayList<classroom_info> db_classroom_3 = new ArrayList<>();//后天的教室数据
    /*
    本地的教室数据
     */
    private ArrayList<classroom_info> local_classroom = new ArrayList<>();//今天的教室数据


    /*
    本地教室数据对应的适配器
     */
    ClassroomInfo_Adapter myAdapter;



    /*
    初始化数据库
    */
    private SQLiteDatabase db = MyDataBaseHelper.getDatabase();


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        info = (user_info) intent.getSerializableExtra("user_info");
        if(info != null){
            Toast.makeText(StudentActivity.this,"成功接受数据",Toast.LENGTH_SHORT).show();
        }else{
            //若无信息，则设置默认值
            info = new user_info();
            info.setId("用户编号");
            info.setPwd("密码");
            info.setName("姓名");
            info.setCharacter("身份");
        }

        myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,local_classroom,info.getName());
        /*
        取教室表数据
         */
        try{

            //从数据库中取出教室表的数据
            db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
            db_classroom_2 = get_db_classroom_infos(db,"classroom2","name_building");
            db_classroom_3 = get_db_classroom_infos(db,"classroom3","name_building");

            //初始的本地数据为数据库中第一天数据
            local_classroom = db_classroom_1;

        }catch (Exception e){
            e.printStackTrace();
            Log.d(Tag,"从数据库中取教室表时出错，来自onCreate");
        }

        /*
        个人中心部分
         */
        setContentView(R.layout.user_center);//先将View设置为个人中心

        //从Intent获取LoginActivity传过来的用户数据，并设置页面信息

        setCenter();


        /*
        查询、申请界面部分
         */
        setSA();
        /*
        !!以下为setSA()函数的部分

        */






    }


    /*
    查询某个表中所有行，用于简化查询
    */
    private Cursor myQuery(SQLiteDatabase database,String table,String order_by){
        try{
            return database.query(table,null,null,null,null,null,order_by);
        }catch (Exception e){
            e.printStackTrace();
            Log.d("StudentActivity","查询某个表时出错，来自函数myQuery");
            return null;
        }
    }


    /*
    从数据库中取出教室数据
    */
    private ArrayList<classroom_info> get_db_classroom_infos(SQLiteDatabase database,String table,String order_by){
        ArrayList<classroom_info> infos = new ArrayList<classroom_info>();
        Cursor cursor = myQuery(database,table,order_by);
        if(cursor.moveToFirst()){
            do{
                classroom_info info = new classroom_info(
                        cursor.getString(cursor.getColumnIndexOrThrow("name_building")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("id_classroom")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class1")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class2")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class3")).toString().trim(),
                        cursor.getString(cursor.getColumnIndexOrThrow("class4")).toString().trim()
                );
                infos.add(info);
            }
            while(cursor.moveToNext());
        }

        return infos;
    }

    /*
    该方法用于设置个人中心里的信息。因为所用控件均为本地变量，所以不可在其他地方复用
     */
    private void setCenter(){

        setContentView(R.layout.user_center);

        user_center_character = findViewById(R.id.user_center_character);
        user_id = findViewById(R.id.user_center_id);
        user_name = findViewById(R.id.user_center_name);
        user_pwd = findViewById(R.id.user_center_pwd);
        user_viewpwd = findViewById(R.id.user_center_viewpwd);
        user_logout = findViewById(R.id.user_center_logout);

        //在页面上显示用户的个人信息
        user_center_character.setText("身份："+info.getCharacter());
        user_id.setText("用户编号："+info.getId());
        user_name.setText("用户名："+info.getName().trim());
        user_pwd.setText(info.getPwd());
        user_viewpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(user_pwd.getVisibility() == View.INVISIBLE){
                    user_pwd.setVisibility(View.VISIBLE);
                }else{
                    user_pwd.setVisibility(View.INVISIBLE);
                }
            }
        });
        user_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /*
    该方法用于设置用户查询申请页面。因为所用控件均为本地变量，所以不可在其他地方复用
     */
    private void setSA(){

        setContentView(R.layout.user_sa);
        spinner_time = findViewById(R.id.user_sa_spinner_time);
        spinner_name_building= findViewById(R.id.user_sa_spinner_name_building);
        spinner_crid = findViewById(R.id.user_sa_spinner_crid);
        spinner_classtime= findViewById(R.id.user_sa_spinner_classtime);
        button_search = findViewById(R.id.user_sa_button_search);
        button_apply = findViewById(R.id.user_sa_button_apply);
        listView = findViewById(R.id.user_sa_listview);
        time_applied = findViewById(R.id.user_sa_time_applied);
        user_sa_textview1 = findViewById(R.id.user_sa_textview1);
        user_sa_textview2 = findViewById(R.id.user_sa_textview2);

        local_classroom = db_classroom_1;
        myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,local_classroom,info.getName());
        listView.setAdapter(myAdapter);
        //获取用户选择的时间
        spinner_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                date_selected = StudentActivity.this.getResources().getStringArray(R.array.time_selected)[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        }
        );
        //获取用户选择的教学楼
        spinner_name_building.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                name_building_selected = StudentActivity.this.getResources().getStringArray(R.array.name_building)[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //获得用户申请的教室crid_selected
        spinner_crid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                crid_selected = id_cr_all.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //获得用户申请的课时classtime_selected
        spinner_classtime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                classtime_selected = StudentActivity.this.getResources().getStringArray(R.array.class_time)[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        /*
        查询按钮的逻辑
         */
        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                id_cr_all.clear();
                Log.d(Tag,"查询按钮被按下");

                if(name_building_selected.equals("全部")){
                    user_sa_textview1.setVisibility(View.INVISIBLE);
                    user_sa_textview2.setVisibility(View.INVISIBLE);
                    spinner_crid.setVisibility(View.INVISIBLE);
                    spinner_classtime.setVisibility(View.INVISIBLE);
                    button_apply.setVisibility(View.INVISIBLE);
                }else{
                    user_sa_textview1.setVisibility(View.VISIBLE);
                    user_sa_textview2.setVisibility(View.VISIBLE);
                    spinner_crid.setVisibility(View.VISIBLE);
                    spinner_classtime.setVisibility(View.VISIBLE);
                    button_apply.setVisibility(View.VISIBLE);
                }

                try{
                    /*
                根据选择的时间调整myAdapter
                 */
                    //今天
                    if(date_selected.equals("今天")){
                        if(name_building_selected.equals("全部")){
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(true){
                                    Log.d(Tag,"查询到教学楼数据，插入中");
                                    tmp.add(info_tmp);
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("荔枝楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("荔枝楼")){
                                    Log.d(Tag,"查询到荔枝楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);

                        } else if (name_building_selected.equals("德寄楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("德寄楼")){
                                    Log.d(Tag,"查询到德寄楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("学会楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("学会楼")){
                                    Log.d(Tag,"查询到学会楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else if (name_building_selected.equals("环宫楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_1){
                                if(info_tmp.getName_building().equals("环宫楼")){
                                    Log.d(Tag,"查询到环宫楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else{
                            Toast.makeText(StudentActivity.this,"不存在的楼名，你是怎么做到的",Toast.LENGTH_SHORT);
                        }
                    }
                    //明天
                    else if (date_selected.equals("明天")) {
                        if(name_building_selected.equals("全部")){
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(true){
                                    Log.d(Tag,"查询到教学楼数据，插入中");
                                    tmp.add(info_tmp);
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("荔枝楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("荔枝楼")){
                                    Log.d(Tag,"查询到荔枝楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);

                        } else if (name_building_selected.equals("德寄楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("德寄楼")){
                                    Log.d(Tag,"查询到德寄楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("学会楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("学会楼")){
                                    Log.d(Tag,"查询到学会楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else if (name_building_selected.equals("环宫楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_2){
                                if(info_tmp.getName_building().equals("环宫楼")){
                                    Log.d(Tag,"查询到环宫楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else{
                            Toast.makeText(StudentActivity.this,"不存在的楼名，你是怎么做到的",Toast.LENGTH_SHORT);
                        }
                    }
                    //后天
                    else if(date_selected.equals("后天")){
                        if(name_building_selected.equals("全部")){
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(true){
                                    Log.d(Tag,"查询到教学楼数据，插入中");
                                    tmp.add(info_tmp);
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("荔枝楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("荔枝楼")){
                                    Log.d(Tag,"查询到荔枝楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);

                        } else if (name_building_selected.equals("德寄楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("德寄楼")){
                                    Log.d(Tag,"查询到德寄楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        } else if (name_building_selected.equals("学会楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("学会楼")){
                                    Log.d(Tag,"查询到学会楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else if (name_building_selected.equals("环宫楼")) {
                            Log.d(Tag,"爆了");
                            ArrayList<classroom_info> tmp = new ArrayList<classroom_info>();
                            for(classroom_info info_tmp : db_classroom_3){
                                if(info_tmp.getName_building().equals("环宫楼")){
                                    Log.d(Tag,"查询到环宫楼数据，插入中");
                                    tmp.add(info_tmp);
                                    id_cr_all.add(info_tmp.getId_classroom());
                                }
                            }
                            Log.d(Tag,"local_classroom生成完毕");
                            myAdapter = new ClassroomInfo_Adapter(StudentActivity.this,tmp,info.getName());
                            listView.setAdapter(myAdapter);
                        }else{
                            Toast.makeText(StudentActivity.this,"不存在的楼名，你是怎么做到的",Toast.LENGTH_SHORT);
                        }
                    }else{
                        Toast.makeText(StudentActivity.this,"不存在的时间，你是怎么做到的",Toast.LENGTH_SHORT);
                    }

                    //更新申请时间
                    time_applied.setText(StudentActivity.user_applied);
                    Log.d(Tag,"user_applied"+user_applied);

                    ArrayAdapter<String> adapter = new ArrayAdapter<>(StudentActivity.this, android.R.layout.simple_expandable_list_item_1,id_cr_all);
                    spinner_crid.setAdapter(adapter);


                }catch (Exception e){
                    e.printStackTrace();
                    Log.d(Tag,"查询按钮逻辑出错");
                }
                Log.d(Tag,"查询按钮逻辑结束");
                Log.d(Tag,"Adapter传过来的user_applied:"+StudentActivity.user_applied);
                }



        });

        //申请按钮的逻辑
        button_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(date_selected.equals("今天")){
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom1",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            String is_vacant = cursor.getString(cursor.getColumnIndexOrThrow(query_class_tmp));

                            //查询申请时间段的占用情况是否为空闲
                            if(is_vacant.equals("空闲")){
                                ContentValues values = new ContentValues();
                                values.put(query_class_tmp,info.getName());
                                db.update("classroom1",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});
                                Log.d(Tag,"596行，数据库数据修改成功");
                                //修改本地数据
                                db_classroom_1 = get_db_classroom_infos(db,"classroom1","name_building");
                            }else{
                                Toast.makeText(StudentActivity.this,"申请的时间段已被占用",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(StudentActivity.this,"申请成功",Toast.LENGTH_SHORT).show();
                } else if (date_selected.equals("明天")) {
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom2",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            String is_vacant = cursor.getString(cursor.getColumnIndexOrThrow(query_class_tmp));

                            //查询申请时间段的占用情况是否为空闲
                            if(is_vacant.equals("空闲")){
                                ContentValues values = new ContentValues();
                                values.put(query_class_tmp,info.getName());
                                db.update("classroom2",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});//修改本地数据
                                db_classroom_2 = get_db_classroom_infos(db,"classroom2","name_building");
                            }else{
                                Toast.makeText(StudentActivity.this,"申请的时间段已被占用",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(StudentActivity.this,"申请成功",Toast.LENGTH_SHORT).show();
                } else if (date_selected.equals("后天")) {
                    String query_class_tmp = "class"+classtime_selected;//用于指定查询哪一列数据，如class1
                    try {
                        Cursor cursor = db.query("classroom3",null,"name_building=? and id_classroom=?",new String[]{name_building_selected,crid_selected},null,null,null);
                        if(cursor.moveToFirst()){
                            String is_vacant = cursor.getString(cursor.getColumnIndexOrThrow(query_class_tmp));

                            //查询申请时间段的占用情况是否为空闲
                            if(is_vacant.equals("空闲")){
                                ContentValues values = new ContentValues();
                                values.put(query_class_tmp,info.getName());
                                db.update("classroom3",values,"name_building=? and id_classroom=? ",new String[]{name_building_selected,crid_selected});//修改本地数据
                                db_classroom_3 = get_db_classroom_infos(db,"classroom3","name_building");
                            }else{
                                Toast.makeText(StudentActivity.this,"申请的时间段已被占用",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Log.d(Tag,"申请按钮逻辑出错");
                    }
                    Toast.makeText(StudentActivity.this,"申请成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(StudentActivity.this,"不存在的申请日期，你是怎么做到的",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    /*
        以下两个方法是关于本Activity使用的菜单。
        onCreateOptionsMenu指定本Activity使用的是user_menu菜单。
        onOptionsItemSelected指定了点击菜单后的事件，即调转到对应页面。
        管理员页面的菜单设置可参考该段代码。
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.user_menu,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.user_menu_sa){
            setSA();
        } else if (id == R.id.user_menu_center) {

            setCenter();
        }else{
            Toast.makeText(StudentActivity.this,"你是怎么做到的",Toast.LENGTH_SHORT).show();
        }
        return true;
    }


}